package com.hdc.mumuwei.micro_account_server.utils;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.hdc.mumuwei.micro_account_server.mapper.TokenMapper;
import com.hdc.mumuwei.micro_account_server.po.Token;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mumuwei
 */
public class TokenUtil {

    private static TokenMapper tokenMapper;

    /**
     * 有效时长毫秒数
     */
    private static final long EXPIRE_TIME = 30 * 60 * 1000;

    /**
     * token 密钥
     */
    private static final String TOKEN_SECRET = "mumuweinfdqwiuALSDFuhbcikueysgfvUYGKU";

    /**
     * @title 生成token
     */
    public static String generateToken(String userId)
    {
        try
        {
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            /**
             * 设置头部信息
             */
            Map<String, Object> header = new HashMap<String, Object>();
            header.put("typ", "JWT");
            header.put("alg", "HS256");
            String token = JWT.create()
                    .withHeader(header)
                    .withClaim("userId", userId)
                    .withExpiresAt(date)
                    .sign(algorithm);
            return token;
        }
        catch (IllegalArgumentException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 校验token
     * @param token token
     * @return 状态 0：校验通过； 1：token过期 2：校验失败
     */
    public static int verify(String token)
    {
        int flag;
        try
        {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWT.require(algorithm).build().verify(token);

            flag = 0;
        }
        catch (TokenExpiredException e)
        {
            e.printStackTrace();
            flag = 1;
        }
        catch (JWTVerificationException e)
        {
            e.printStackTrace();
            flag = 2;
        }
        return flag;
    }

    /**
     * @title 获取token中的用户id
     */
    public static String getUserId(String token)
    {
        DecodedJWT jwt = JWT.decode(token);
        return jwt.getClaim("userId").asString();
    }

//    public static Result verifyToken(Token token){
//        Result result = null;
//         int flag = TokenUtil.verify(token.getToken());
//         if(flag == 0){
//
//         } else if (flag == 1){
//
//         } else {
//
//         }
//
////        String token = tokenMapper.getTokenByUid(tk.getUid());
////        if(token != null){
////            if (tk.getToken().equals(token)){
////                result = new Result(Result.ERR_TOKEN_CODE, Result.TOKEN_ERR_MSG);
////            }else {
////                System.err.println("token失效");
////            }
////        }else {
////            result = new Result(Result.ERR_TOKEN_CODE, Result.TOKEN_ERR_MSG);
////        }
//        return result;
//    }
}